package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateChannelTaskEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TaskName    string // 模板名称
	ChannelName string // 设备厂商名称
	ChannelType string // 设备类型名
	SendMode    string // 设备类型名
	Timeout     int64  // 设备类型名

	TaskParam map[string]interface{} // 模板参数
}

type operateChannelTaskPo struct {
	Id          int64                  `json:"id" db:"id"`                    // 对象id
	CreateTime  int64                  `json:"createTime" db:"create_time"`   // 创建时间
	UpdateTime  int64                  `json:"updateTime" db:"update_time"`   // 更新时间
	ChannelName string                 `json:"channelName" db:"channel_name"` // 设备厂商名称
	ChannelType string                 `json:"channelType" db:"channel_type"` // 设备类型名
	SendMode    string                 `json:"sendMode" db:"send_mode"`       // 设备类型名
	Timeout     int64                  `json:"timeout" db:"timeout"`          // 设备类型名
	TaskName    string                 `json:"taskName" db:"task_name"`       // 设备名称
	TaskParam   map[string]interface{} `json:"taskParam" db:"task_param"`     // 配置参数
}

func (e *OperateChannelTaskEntity) Type() string {
	return "OperateChannelTaskEntity"
}

func (e *OperateChannelTaskEntity) TableName() string {
	return "tb_operate_channel_task"
}

func (e *OperateChannelTaskEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateChannelTaskEntity) Instance() *OperateChannelTaskEntity {
	return &OperateChannelTaskEntity{
		BaseEntity: new(commEntity.BaseEntity),
		TaskParam:  make(map[string]interface{}),
	}
}

func (e *OperateChannelTaskEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateChannelTaskEntity) NewPo() interface{} {
	return &operateChannelTaskPo{
		TaskParam: make(map[string]interface{}),
	}
}

func (e *OperateChannelTaskEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateChannelTaskEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.TaskName)
	return p
}

func (e *OperateChannelTaskEntity) BuildByJson(jsn *string) error {
	var po operateChannelTaskPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateChannelTaskEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateChannelTaskEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operateChannelTaskPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TaskParam = po.TaskParam
	ptr.ChannelName = po.ChannelName
	ptr.ChannelType = po.ChannelType
	ptr.SendMode = po.SendMode
	ptr.Timeout = po.Timeout
	ptr.TaskName = po.TaskName
}

func (e *OperateChannelTaskEntity) BuildToPo() interface{} {
	ptr := e
	po := operateChannelTaskPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TaskParam = ptr.TaskParam
	po.ChannelName = ptr.ChannelName
	po.ChannelType = ptr.ChannelType
	po.SendMode = ptr.SendMode
	po.Timeout = ptr.Timeout
	po.TaskName = ptr.TaskName

	return &po
}
